package com.yinda.dt.produce.utils;

import android.graphics.Color;
import android.graphics.drawable.Drawable;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.AxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/6 0006.
 */
public class LineChartUtil {

    public static void showChart(LineChart lineChart, final ArrayList<String> xData, ArrayList<String> yData, MarkerView markerView, Drawable drawable){
        lineChart.setMarkerView(markerView);
        XAxis x = lineChart.getXAxis();
        x.setDrawAxisLine(false);
        x.setGridColor(Color.parseColor("#11000000"));
        x.setTextColor(Color.parseColor("#A5A5A5"));
        x.setTextSize(9);
        x.setPosition(XAxis.XAxisPosition.BOTTOM);
        x.setValueFormatter(new AxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xData.get((int)value);
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });
        lineChart.getAxisRight().setDrawLabels(false);
        lineChart.getAxisRight().setGridColor(Color.TRANSPARENT);
        lineChart.getAxisRight().setAxisLineColor(
                Color.parseColor("#F5F5F5"));


        YAxis y = lineChart.getAxisLeft();
        y.setGridColor(Color.TRANSPARENT);
        y.setTextColor(Color.parseColor("#A5A5A5"));
        y.setValueFormatter(new AxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return (int)value+"";
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }


        });
        y.setAxisLineColor(Color.parseColor("#F5F5F5"));
        y.setAxisMinValue(0f);
        y.setLabelCount(6, false);
        y.setXOffset(5);

        ArrayList<Entry> mLineEntries = new ArrayList<Entry>();
        for (int i = 0; i < yData.size(); i++) {
            float value = Float.parseFloat(yData.get(i));
            mLineEntries.add(new Entry(i, value));
        }
        LineDataSet set = new LineDataSet(mLineEntries, "");
        set.setDrawValues(false);

        set.setFillDrawable(drawable);
        lineChart.setMarkerView(markerView);
        set.setDrawFilled(true);

        set.setLineWidth(1.75f); // 线宽
        set.setCircleRadius(3f);// 显示的圆形大小
        set.setColor(Color.parseColor("#F79029"));// 显示颜色
        set.setCircleColor(Color.parseColor("#F79029"));// 圆形的颜色

        set.setHighLightColor(Color.TRANSPARENT);
        ArrayList<ILineDataSet> mLineDataSets = new ArrayList<>();
        mLineDataSets.add(set);
        LineData mLineData = new LineData(mLineDataSets);
        lineChart.setData(mLineData);


        lineChart.setDescription("");
        lineChart.setNoDataTextDescription("本月暂无数据");
        lineChart.setScaleEnabled(false);
        lineChart.setDragEnabled(false);
        lineChart.setPinchZoom(false);
        lineChart.getLegend().setEnabled(false);
        lineChart.setDescription("");
    }

}
